import axios from "axios";
import store from "../store";
import ENV from "./env";

import { getToken, getWxAuth } from "./auth";
import { MessageBox, Toast } from "mint-ui";

// 创建axios实例
const channelQ = axios.create({
  baseURL: ENV.CHANNEL_API, // api的base_url
  timeout: 100000, // 请求超时时间
});

// request拦截器
channelQ.interceptors.request.use(
  (config) => {
    const token = getToken();
    config.headers["Authorization"] = `Bearer ${token}`;
    config.headers["Content-Type"] = "application/json";
    return config;
  },
  (error) => {
    console.log(error); // for debug
    Promise.reject(error);
  }
);

// respone拦截器
channelQ.interceptors.response.use(
  (response) => {
    if (response.data.code === 5) {
      // store.dispatch('LogOut').then(() => {
      //   getWxAuth();
      // });
      location.replace("/doctor/loginPhone");
    } else if (response.data.code === 0) {
      return Promise.resolve(response.data);
    } else if (
      response.data.code === 1021 ||
      response.data.code === 1021 ||
      response.data.code === 1022
    ) {
      return Promise.reject(response.data);
    } else if (response.data.code === 1218) {
      Toast("最多只能查看二级渠道");
      return Promise.reject(response.data.msg);
    } else {
      return Promise.reject(response.data.msg);
    }
  },
  (error) => {
    // alert(error)
    console.log("err" + error); // for debug
    return Promise.resolve(error);
  }
);

export default channelQ;
